<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item :label="$t('业务时间')">
              <j-date
                :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('开始时间')"
                class="query-group-cust"
                v-model="queryParam.busiTimeBeg"
              ></j-date>
              <span class="query-group-split-cust"></span>
              <j-date
                :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('结束时间')"
                class="query-group-cust"
                v-model="queryParam.busiTimeEnd"
              ></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('业务员信息')">
              <a-input :placeholder="$t('请输入手机号/姓名')" v-model="queryParam.salesmanInfo"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('业务员类型')">
                <j-dict-select-tag
                  :placeholder="$t('请选择业务员类型')"
                  v-model="queryParam.salesmanType"
                  dictCode="bis_salesman.type"
                />
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="8" :lg="9" :md="10" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{$t('查询')}}</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{$t('重置')}}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? $t('收起') : $t('展开') }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
              <a-button type="primary" icon="download" :loading="exloading" style="margin-left: 8px" @click="handleExportXls($t('分销统计'))" v-has="'RetailStatisticsController:export'"
                >{{$t('导出')}}</a-button
              >
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="customerNum" slot-scope="text, record">
          <a @click="orderDetail(record)">{{ text }}</a>
        </template>
        <template slot="orderPrice" slot-scope="text, record">
          <a @click="priceDetail(record)">{{ text.toFixed(2) }}</a>
        </template>
        <template slot="footer">
          <div style="font-weight: bold">
            <span style="margin-left: 15px; margin-right: 5px">{{$t('管理客户数量')}}：{{ total.customerNum }}</span>
            <span style="margin-left: 15px; margin-right: 5px">{{$t('订单金额')}}：{{ total.orderPrice.toFixed(2) }}</span>
            <span style="margin-left: 15px; margin-right: 5px">{{$t('扣币')}}：{{ total.coinValue }}</span>
            <span style="margin-left: 15px; margin-right: 5px">{{$t('首单金额')}}：{{ total.firstSumPrice }}</span>
          </div>
        </template>
      </a-table>
    </div>
  </a-card>
</template>

<script>
import moment from 'moment'
import '@/assets/less/TableExpand.less'
import { getAction } from '@/api/manage'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'

export default {
  name: 'DistributionStatistics',
  mixins: [JeecgListMixin, mixinDevice],
  components: {},
  data() {
    return {
      moment,
      description: this.$t('分销统计'),
      queryParam: {
        busiTimeBeg: '',
        busiTimeEnd: '',
        salesmanInfo: '',
        salesmanType: undefined,
      },
      // 表头
      columns: [
        {
          title: this.$t('业务员手机号'),
          align: 'center',
          dataIndex: 'salesmanPhone',
        },
        {
          title: this.$t('业务员姓名'),
          align: 'center',
          dataIndex: 'salesmanName',
        },
        {
          title: this.$t('业务员类型'),
          align: 'center',
          dataIndex: 'salesmanType_dictText',
        },
        {
          title: this.$t('管理客户数量'),
          align: 'center',
          dataIndex: 'customerNum',
          scopedSlots: { customRender: 'customerNum' },
        },
        {
          title: this.$t('订单金额'),
          align: 'center',
          dataIndex: 'orderPrice',
          customCell:()=>{
            return {
              style:{
                'textAlign': 'right'
              }
            }
          },
          scopedSlots: { customRender: 'orderPrice' },
        },
        {
          title: this.$t('扣币'),
          align: 'center',
          dataIndex: 'coinValue',
          customCell:()=>{
            return {
              style:{
                'textAlign': 'right'
              }
            }
          },
          customRender: (text, r) => {
            return text?parseFloat(text).toFixed(2):0.00.toFixed(2);
          }
        },
        {
          title: this.$t('首单金额'),
          align: 'center',
          dataIndex: 'firstSumPrice',
          customRender: (text, r) => {
            return text?parseFloat(text).toFixed(2):0.00.toFixed(2);
          },
          customCell:()=>{
            return {
              style:{
                'textAlign': 'right'
              }
            }
          },
        },
      ],
      url: {
        list: '/retail/retailStatistics/list',
        exportXlsUrl: '/retail/retailStatistics/exportXls',
      },
      dictOptions: {},
      total: {
        customerNum: 0,
        orderPrice: 0,
        coinValue: 0,
        firstSumPrice: 0
      },
    }
  },
  created() {},
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    initDictConfig() {
      this.getTotal()
    },
    searchQuery() {
      this.loadData(1)
      this.getTotal()
    },
    getTotal() {
      getAction('/retail/retailStatistics/total', this.queryParam).then((res) => {
        if (res.success) {
          if (res.result) {
            this.total = res.result
          } else {
            this.total = {
              customerNum: 0,
              orderPrice: 0,
              coinValue: 0,
            }
          }
        } else {
          this.$$message.warning(res.message)
        }
      })
    },
    searchReset() {
      this.queryParam = {
        busiTimeBeg: '',
        busiTimeEnd: '',
        salesmanInfo: '',
        salesmanType: undefined,
      }
      this.loadData(1)
      this.getTotal()
    },
    orderDetail(record) {
      let query = {
        salesmanId: record.salesmanId,
      }
      this.$router.push({ path: '/member/MemberManage', query: query })
    },
    priceDetail(record){
      let query = {
        salesmanInfo: record.salesmanPhone
      }
      if(this.queryParam.busiTimeBeg&&this.queryParam.busiTimeEnd){
        query.busiTimeBeg=this.queryParam.busiTimeBeg
        query.busiTimeEnd=this.queryParam.busiTimeEnd
      }
      this.$router.push({ path: '/member/DistributionDetail', query: query })
    }
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>